package com.example.controller;

import com.example.dao.UserCrudRepository;
import com.example.dao.VideoLearningRecordsCrudRepository;
import com.example.pojo.User;
import com.example.pojo.VideoLearningRecords;
import com.example.pojo.PageBean;
import com.example.tools.JwtUtils;
import com.example.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/videoLearningRecords")
@Slf4j
public class VideoLearningRecordsController {
    @Autowired
    private VideoLearningRecordsCrudRepository videoLearningRecordsCrudRepository;
    @Autowired
    private UserCrudRepository userCrudRepository;

    @PostMapping("/addVideoLearningRecords")
    public Result add(Integer trainingPlanId, Integer documentVideoId,Integer time, @RequestHeader("token") String token) {
        Integer id = JwtUtils.parseJWT(token).get("id", Integer.class);
        User user = userCrudRepository.findById(id).get();
        VideoLearningRecords times = videoLearningRecordsCrudRepository
                .getVideoLearningRecordsByTrainingPlanIdAndDocumentVideoIdAndUserId(trainingPlanId, documentVideoId, user.getId());
        if(times!=null){
            times.setVideoTime(time);
            videoLearningRecordsCrudRepository.save(times);
        }else {
            VideoLearningRecords videoLearningRecords = new VideoLearningRecords(null, user.getId(), trainingPlanId, documentVideoId, time);
            videoLearningRecordsCrudRepository.save(videoLearningRecords);
        }
        return Result.success();
    }

    @DeleteMapping("/deleteVideoLearningRecords")
    public Result delete(@RequestBody List<Integer> ids) {

        for (Integer id : ids) {
            videoLearningRecordsCrudRepository.deleteById(id);
        }
        return Result.success("删除成功");
    }

    //获取某人的某个计划的某个视频的播放时间
    @GetMapping("/getVideoLearningRecords")
    public Result getVideoLearningRecords(Integer trainingPlanId, Integer documentVideoId, @RequestHeader("token") String token) {
        Integer id = JwtUtils.parseJWT(token).get("id", Integer.class);
        User user = userCrudRepository.findById(id).get();
        VideoLearningRecords time = videoLearningRecordsCrudRepository
                .getVideoLearningRecordsByTrainingPlanIdAndDocumentVideoIdAndUserId(trainingPlanId, documentVideoId, user.getId());
        if(time!=null){
            return Result.success(time.getVideoTime());
        }
       else {
           return Result.success();
        }
    }

}
